package com.ccc.boot.system.controller;

import com.ccc.boot.constant.CommonConstant;
import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.SysDict;
import com.ccc.boot.system.service.SysDictInfoService;
import com.ccc.boot.system.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 〈数据字典——REST接口〉
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/3/29
 */
@RestController
@RequestMapping(CommonConstant.SYS_URI_PREFIX + "/dict")
public class SysDictController {

    protected static final Logger logger = LoggerFactory.getLogger(SysDictController.class);

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictInfoService sysDictInfoService;

    /**
     * 新增
     */
    @PostMapping()
    public ResponseResult add(@RequestBody SysDict sysDict) {
        // 业务逻辑
        boolean created = sysDictService.add(sysDict);
        if (created) {
            return ResponseResult.success("新增成功");
        }

        return ResponseResult.failure(ResponseCode.DATA_ADD_FAILURE);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable String id) {
        // 业务逻辑
        boolean deleted = sysDictService.delete(id);
        if (deleted) {
            return ResponseResult.success(ResponseCode.DATA_DELETE_SUCCESS);
        }

        return ResponseResult.failure(ResponseCode.DATA_DELETE_FAILURE);
    }

    /**
     * 修改
     */
    @PutMapping()
    public ResponseResult update(@RequestBody SysDict sysDict) {
        // 业务逻辑
        boolean updated = sysDictService.update(sysDict);
        if (updated) {
            return ResponseResult.success("修改成功");
        }

        return ResponseResult.failure(ResponseCode.DATA_UPDATE_FAILURE);
    }

    /**
     * 查询
     */
    @GetMapping("/getList")
    public ResponseResult getList() {
        List<SysDict> list = sysDictService.getList();
        return ResponseResult.success("查询成功", list);
    }

    /**
     * 根据dictIds
     *
     * @return {@link ResponseResult}
     * @date 2022/4/14 22:23
     * @author superC
     */

    @GetMapping("getAllDictData")
    public ResponseResult getAllDictData() {
        try {
            Map<String, Map<String, Object>> resultMap = sysDictService.getAllDictData();
            return ResponseResult.success("获取数据字典数据成功", resultMap);
        } catch (Exception e) {
            logger.error("【捕获异常-获取数据字典】\r\n异常记录：", e);
        }
        return ResponseResult.failure("获取数据字典失败");
    }

    /**
     * 根据dictIds,获取数据字典
     *
     * @param dictIds 关键字串，用英文逗号隔开
     * @return {@link ResponseResult}
     * @date 2022/4/10 21:32
     * @author superC
     */
    @GetMapping("getDictData")
    public ResponseResult getDictData(@RequestParam String dictIds) {
        try {
            if (dictIds == null) {
                return ResponseResult.failure(ResponseCode.PARAM_IS_BLANK);
            }
            Map<String, Map<String, Object>> resultMap = sysDictInfoService.getDictData(dictIds);
            return ResponseResult.success("获取数据字典数据成功", resultMap);
        } catch (Exception e) {
            logger.error("【捕获异常-获取数据字典】\r\n异常记录：", e);
        }
        return ResponseResult.failure("获取数据字典失败");
    }

}
